package com.ibeeking.found.cms.cp.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.cms.cp.rest.service.IPageComponentsGroupService;
import com.ibeeking.found.cms.service.common.dos.PageComponentsGroupDO;
import com.ibeeking.found.cms.service.common.query.PageComponentsGroupQuery;
import com.ibeeking.found.cms.service.common.vo.PageComponentsGroupVO;
import com.ibeeking.found.cms.service.mapper.PageComponentsGroupMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName PageComponentsGroupServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class PageComponentsGroupServiceImpl  extends ServiceImpl<PageComponentsGroupMapper, PageComponentsGroupDO> implements IPageComponentsGroupService {

    @Resource
    private PageComponentsGroupMapper pageComponentsGroupMapper;

    @Override
    public List<PageComponentsGroupVO> list(PageComponentsGroupQuery query) {
        List<PageComponentsGroupDO> entityDOS = pageComponentsGroupMapper.selectList(new LambdaQueryWrapper<PageComponentsGroupDO>()
                .eq(query.getId() != null, PageComponentsGroupDO::getId, query.getId())
                .eq(StringUtils.isNotBlank(query.getIndustry()), PageComponentsGroupDO::getIndustry, query.getIndustry())
                .eq(StringUtils.isNotBlank(query.getCode()), PageComponentsGroupDO::getCode, query.getCode())
                .like(StringUtils.isNotBlank(query.getName()), PageComponentsGroupDO::getName, query.getName())
                .orderByDesc(PageComponentsGroupDO::getSort)
        );
        List<PageComponentsGroupVO> entityVOS =JsonUtils.jsonToList(entityDOS, PageComponentsGroupVO.class);
        return entityVOS;
    }

    @Override
    public PageComponentsGroupVO view(Long id) {
        PageComponentsGroupDO entityDO = pageComponentsGroupMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        PageComponentsGroupVO entityVO = BeanUtil.convertBean(entityDO, PageComponentsGroupVO.class);
        return entityVO;
    }

}
